<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@ page import="java.sql.*" %>
<%@ page import="bean.DB" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>修改商品信息</title>
    <style>
        table {
            border-collapse: collapse;
            width: 60%;
            margin: 20px auto;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: center;
        }
        th {
            background-color: #4CAF50;
            color: white;
        }
        tr:nth-child(even) {
            background-color: #f2f2f2;
        }
        tr:hover {
            background-color: #ddd;
        }
        form {
            margin: 20px auto;
            width: 60%;
            text-align: center;
        }
        label {
            display: block;
            margin: 10px 0;
        }
        input[type="text"], input[type="number"], textarea {
            width: 100%;
            padding: 8px;
            margin: 5px 0;
            box-sizing: border-box;
        }
        input[type="submit"], input[type="button"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            cursor: pointer;
        }
        input[type="submit"]:hover, input[type="button"]:hover {
            background-color: #45a049;
        }
    </style>
</head>
<body>
<h1 style="text-align: center;">修改商品信息</h1>
<%
    String productId = request.getParameter("id"); // 获取商品 ID
    DB db = new DB();
    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    String productName = "";
    String price = "";
    String description = "";
    String star = "";
    String imagePath = "";

    try {
        conn = db.createConn();
        String sql = "SELECT product_name, price, description, star, image_path FROM products WHERE id = ?";
        pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, productId);
        rs = pstmt.executeQuery();

        if (rs.next()) {
            productName = rs.getString("product_name");
            price = rs.getString("price");
            description = rs.getString("description");
            star = rs.getString("star");
            imagePath = rs.getString("image_path");
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        try {
            if (rs != null) rs.close();
            if (pstmt != null) pstmt.close();
            if (conn != null) conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
%>
<form method="post" action="./updateProductServlet">
    <input type="hidden" name="id" value="<%= productId %>">
    <label for="product_name">商品名：</label>
    <input type="text" id="product_name" name="product_name" value="<%= productName %>" required>

    <label for="price">价格：</label>
    <input type="number" step="0.01" id="price" name="price" value="<%= price %>" required>

    <label for="description">商品描述：</label>
    <textarea id="description" name="description" rows="4" required><%= description %></textarea>

    <label for="star">积分：</label>
    <input type="number" id="star" name="star" value="<%= star %>" required>

    <label for="image_path">图片路径：</label>
    <input type="text" id="image_path" name="image_path" value="<%= imagePath %>" required>

    <div style="margin-top: 20px;">
        <input type="submit" value="提交修改">
        <input type="button" value="返回列表" onclick="window.location.href='productPage.jsp';">
    </div>
</form>
</body>
</html>
